define(['jquery','tyutil', 'Constants'],function($,tyutil,Constants){
	var userState = 0;
	return {
		load: function() {
			alert(1);
			document.ontouchmove = function(ev){
				ev.preventDefault();
			};
			var heightVal = document.body.scrollHeight - 145;
			$('#iframeId').css('height', heightVal + "px");
			/*var url = "/SalesSystem/cardManagerAction.do?method=getSessionChannel&time="+Date.parse(new Date());
			var dataParam = {
			};
			tyutil.doAjax(url, "post", false, dataParam, function(data) {
				if(data.state != 0 && !tyutil.isEmpty(data)) {
					$("#mainTitleId").html(data.subChannel);
				}
			});*/
			init();
		}
	};
	function addCardTypeClick() {
		//document.getElementById("mainContent").src='/SalesSystem/mobilejsp/add_cardType.jsp';
	}
	//新的正在使用的一版
	function init() {
		getCurUserType();
		/*$('#topBackId').css('display', 'none');
		$.ajax({
			url:'/SalesSystem/menuAction.do?method=getUserMenu&time='+Date.parse(new Date()),  
		    type: 'post',
		    async: true,  
		    dataType: 'json',  
		    success:function(data, textStatus){  
		    	var sortArr = new Array();
		    	for(var key in data) {
		    		if(data[key].node_id == 1403) {
		    			var value = data[key];
		    			delete data[key];
		    			data[0] = value;
		    		}
		    	}
		    	var gonggaobanText = "公告板";  //默认进来是公告板
		    	var gonggaobanUrl = "";
		    	for(var key in data) {
		    		if(data[key].sub_node) {
		    			for(var secondKey in data[key].sub_node) {		//二级菜单
		    				var secondNode = data[key]['sub_node'][secondKey];
		    				$('#mm').menu('appendItem', {
		    				    //parent: item.target,  // the parent item element
		    				    text: secondNode['node_name'],
		    				    id: secondNode['node_id'],
		    					url: secondNode['node_url']
		    				});
		    				if(gonggaobanText == secondNode['node_name']) {
		    					//gonggaobanUrl = secondNode['node_url'];
		    				}
		    				//--------实现不同平台的click监听事件----------------
		    				if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
		    				       $('#' +secondNode['node_id']).bind({"touchend click": function(){
				    				    // 下面这行代码就是你要的ID属性
				    					var urlId = $(this).attr("id");
				    					openUrl(urlId, data);
				    				}});
		    				   } else {
		    					   $('#' +secondNode['node_id']).click(function(){
				    				    // 下面这行代码就是你要的ID属性
				    					var urlId = $(this).attr("id");
				    					openUrl(urlId, data);
				    				});
		    				   }
		    				$("div .menu-text").css({fontSize:"18px"});
		    				$("div .menu-text span").css({fontSize:"18px"});
		    			}
		    		}
		    	}
		    	//document.getElementById("mainContent").src='/SalesSystem/mobile' + gonggaobanUrl;
		    	document.getElementById("mainContent").src='/SalesSystem/mobilejsp/gonggaobanlist.jsp';
		    },
		    error:function (XMLHttpRequest, textStatus, errorThrown){
		    	alert("菜单数据加载异常！");
		    }
		});*/
	//	$('#mm').menu({itemHeight: 38});
	}
	//判断当前账号类型
	function getCurUserType() {
		var url = "/crm/admin/users_info.html";
		var dataParam = {
		};
		tyutil.doAjax(url, "post", false, dataParam, function(data) {
			var json = eval("("+data+")");
			console.info(data);
			alert(json.state);
			if(json.state != 0) {
				var rows = data.rows;
				userState = rows[0].state;
				if(rows[0].userType == 1) {
					$('#userTip').html('代理商，您好！');
    				$('#roleTopDiv').html('代理商管理');
				} else if(rows[0].userType == 2 ) {
					$('#userTip').html('推广员，您好！');
    				$('#roleTopDiv').html('代理商管理');
				} else if(rows[0].userType == 3) {
					$('#userTip').html('管理员，您好！');
    				$('#roleTopDiv').html('合作方管理');
				} else if(rows[0].userType == 4) {
					$('#userTip').html('合作方，您好！');
    				$('#roleTopDiv').html('推广员管理');
				}
				
				if(userState == 0) {
					var url = "/SalesSystem/userInfoAction.do?method=getUserInfoList&time="+Date.parse(new Date());
					var dataParam = {};
					var superOrPromo = '';
					if(!tyutil.isEmpty(rows[0].promoterName)) {
						superOrPromo = rows[0].promoterName;
					} else {
						if(!tyutil.isEmpty(rows[0].superiorUserName)) {
							superOrPromo = rows[0].superiorUserName;
						} 
					}
					if(!tyutil.isEmpty(superOrPromo)) {
						dataParam = {
			    			'agentNameSelect': superOrPromo
			    		};
			    		tyutil.doAjax(url, "post", false, dataParam, function(dataPro) {
			    			if(dataPro.state != 0 && !tyutil.isEmpty(dataPro)) {
			    				if(dataPro.total > 0) {
			    					var rowPro = dataPro.rows[0];
			    					buttonInit(rowPro.phoneNumber);
			    				} else {
			    					buttonInit();
			    				}
			    			}
			    		});
					} else {
						buttonInit();
					}
				} else {
					buttonInit();
				}
			}
		});
	}
	//按钮事件绑定
	function buttonInit(phoneNumber) {
		alert(userState);
		alert(phoneNumber);
		if(userState != 0) {
			$('#cardBuyClickId').click(function() {
				document.getElementById("mainContent").src='/SalesSystem/mobilejsp/card_main.jsp';
			});
			$('#agentManagerClickId').click(function() {
				document.getElementById("mainContent").src='/SalesSystem/mobilejsp/agent_main.jsp';
			});
			$('#gonggaoClickId').click(function() {
				document.getElementById("mainContent").src='/SalesSystem/mobilejsp/gonggaobanlist.jsp';
			});
		} else {
			$('#cardBuyClickId').click(function() {
				alert("您的账号尚未激活，请联系推广员手机号" + phoneNumber);
			});
			$('#agentManagerClickId').click(function() {
				alert("您的账号尚未激活，请联系推广员手机号" + phoneNumber);
			});
			$('#gonggaoClickId').click(function() {
				alert("您的账号尚未激活，请联系推广员手机号" + phoneNumber);
			});
		}
		$('#personalClickId').click(function() {
			document.getElementById("mainContent").src='/SalesSystem/mobilejsp/personal_center.jsp';
		});
		
		
	}
	//已经废弃的一版本
	function initbak() {
		$('#logoutId').click(function() {
			top.document.location.href = "/SalesSystem/userAction.do?method=logout&time="+Date.parse(new Date());
		});
		$.ajax({
			url:'/SalesSystem/menuAction.do?method=getUserMenu&time='+Date.parse(new Date()),  
		    type: 'post',
		    async: true,  
		    dataType: 'json',  
		    success:function(data, textStatus){  
		    	var sortArr = new Array();
		    	for(var key in data) {
		    		if(data[key].node_id == 1403) {
		    			var value = data[key];
		    			delete data[key];
		    			data[0] = value;
		    		}
		    	}
		    	var gonggaobanText = "给玩家充卡";  //以前默认进来是公告板，现在改成默认进来是给玩家充卡
		    	var gonggaobanUrl = "";
		    	for(var key in data) {
		    		if(data[key].sub_node) {
		    			for(var secondKey in data[key].sub_node) {		//二级菜单
		    				var secondNode = data[key]['sub_node'][secondKey];
		    				$('#mm').menu('appendItem', {
		    				    //parent: item.target,  // the parent item element
		    				    text: secondNode['node_name'],
		    				    id: secondNode['node_id'],
		    					url: secondNode['node_url']
		    				});
		    				if(gonggaobanText == secondNode['node_name']) {
		    					gonggaobanUrl = secondNode['node_url'];
		    				}
		    				//--------实现不同平台的click监听事件----------------
		    				if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
		    				       $('#' +secondNode['node_id']).bind({"touchend click": function(){
				    				    // 下面这行代码就是你要的ID属性
				    					var urlId = $(this).attr("id");
				    					openUrl(urlId, data);
				    				}});
		    				   } else {
		    					   $('#' +secondNode['node_id']).click(function(){
				    				    // 下面这行代码就是你要的ID属性
				    					var urlId = $(this).attr("id");
				    					openUrl(urlId, data);
				    				});
		    				   }
		    				$("div .menu-text").css({fontSize:"18px"});
		    				$("div .menu-text span").css({fontSize:"18px"});
		    			}
		    		}
		    	}
		    	document.getElementById("mainContent").src='/SalesSystem/mobile' + gonggaobanUrl;
		    },
		    error:function (XMLHttpRequest, textStatus, errorThrown){
		    	alert("菜单数据加载异常！");
		    }
		});
		$('#mm').menu({itemHeight: 38});
	}
	function ss() {
		$('#mm').menu('appendItem', {
		    text: 'sss',
		    id: "ssID"
		},{
			separator: true
		});
		var item = $('#mm').menu('findItem', 'sss');  // find 'sss' item
		$('#mm').menu('appendItem', {
		    parent: item.target,  // the parent item element
		    text: 'Open Excelyyyyzzzzzzzzzz',
		    iconCls: 'icon-excelyyyzzzzzzzzzzzzzz',
		    onclick: function(){alert('Open Excelyyyzzzzzzzzz');}
		});
		$('#mm').menu('appendItem', {
		    parent: item.target,  // the parent item element
		    text: 'Open Excelyyyy',
		    iconCls: 'icon-excelyyyy',
		    onclick: function(){alert('Open Excelyyy');}
		});
	}
	function openUrl(keyID, data) {
		for(var key in data) {
    		if(data[key].sub_node) {
    			for(var secondKey in data[key].sub_node) {		//二级菜单
    				var secondNode = data[key]['sub_node'][secondKey];
    				if(keyID == secondNode['node_id']) {
    					var nodeUrl = secondNode['node_url'];
    					document.getElementById("mainContent").src='/SalesSystem/mobile'+nodeUrl; 
    				}
    			}
    		}
		}
		$("#mm").menu("hide");
	}
	
	function currentSelectCity(str) {
		/*var index = str.indexOf("_");
		var cardType = str.substr(0,index);
		var completeChannel = str.substr(index + 1,str.length);
		var url = "/SalesSystem/userInfoAction.do?method=currentSelectCity&time="+Date.parse(new Date());
		var dataParam = {
				"cardType" : cardType,
				"completeChannel" : completeChannel
		};
		tyutil.doAjax(url, "post", false, dataParam, function(data) {
			document.getElementById("mainContent").src = '/SalesSystem/mobilehtml/gonggaoban/gonggaobanView.html';
		});*/
	}
});